/**
 * 
 */
package com.example.radioclub.music;

import java.util.ArrayList;
import org.springframework.web.client.RestTemplate;

import com.example.radioclub.R;
import com.example.radioclub.data.Constants;
import com.example.radioclub.music.adapter.MusicAdapter;
import com.example.radioclub.music.adapter.SearchAdapter;
import com.example.radioclub.music.helper.ClientRequestFactory;
import com.example.radioclub.music.interfaces.IRestMusic;
import com.example.radioclub.music.tasks.RestClientAuthDownloader;
import com.example.radioclub.music.tasks.RestGenreDownloader;
import com.example.radioclub.music.tasks.RestMusicDownloader;
import com.example.radioclub.music.tasks.RestSearchDownloader;
import com.example.radioclub.types.ChartSong;
import com.example.radioclub.utilities.GenreChooser;
import com.example.radioclub.utilities.MelonApiURLsTools;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This the ruler of Music and anything with it. Be aware!
 * 
 * @author wizard
 * 
 */
public class MusicActivity extends Activity implements OnClickListener, IRestMusic {

	private final String TAG = Constants.APP_TAG + this.getClass().getName();
	private RestTemplate melOnUserRestTemplate = null;
	private RestTemplate melOnClientRestTemplate = null;
	private ProgressDialog progressDialog;
	private String channelCode = null;

	private Button dailyChartButton, weeklyChartButton, monthlyChartButton;
	private Button searchButton, allMusicsButton, newMusicsButton;
	private TextView genreBox;
	private EditText keywordBox;
	private ListView musicListView;
	private WebView playerView;

	private RestClientAuthDownloader restAuthTask;
	private RestMusicDownloader restMusicDownloader;
	private RestSearchDownloader restSearchDownloader;
	private RestGenreDownloader restGenreDownloader;
	private GenreChooser genreChooser;

	private ArrayList<ChartSong> musicList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());

		super.onCreate(savedInstanceState);

		if(Constants.USER_LOGIN_STATUS == true && Constants.CLIENT_LOGIN_STATUS == true) {
			Log.i(TAG, "User and Client have been verified. Please proceed.");
			
			// Create instance of RestTemplate class for user to access MelOn private services
			String cookie = getIntent().getExtras().getString("melon_user_cookie"); 
			this.melOnUserRestTemplate = new RestTemplate(true, new ClientRequestFactory("Cookie", cookie));
			
			// Create instance of RestTemplate class for client or app to access MelOn private services
			cookie = getIntent().getExtras().getString("melon_client_cookie");
			this.melOnClientRestTemplate = new RestTemplate(true, new ClientRequestFactory("Cookie", cookie));
			
			// Initializing layout
			setContentView(R.layout.music_list_layout);
			
			this.allMusicsButton = (Button) findViewById(R.id.all_music_button);
			this.allMusicsButton.setOnClickListener(this);
			this.newMusicsButton = (Button) findViewById(R.id.new_music_button);
			this.newMusicsButton.setOnClickListener(this);
			this.dailyChartButton = (Button) findViewById(R.id.daily_chart_music_button);
			this.dailyChartButton.setOnClickListener(this);
			this.weeklyChartButton = (Button) findViewById(R.id.weekly_chart_music_button);
			this.weeklyChartButton.setOnClickListener(this);
			this.monthlyChartButton = (Button) findViewById(R.id.monthly_chart_music_button);
			this.monthlyChartButton.setOnClickListener(this);
			this.searchButton = (Button) findViewById(R.id.search_button);
			this.searchButton.setOnClickListener(this);
			this.genreBox = (TextView) findViewById(R.id.genre_box);
			this.genreBox.setOnClickListener(this);
			this.keywordBox = (EditText) findViewById(R.id.keyword_box);
			this.musicListView = (ListView) findViewById(R.id.music_list);
			this.playerView = (WebView) findViewById(R.id.player_webview);

		}
		
		if(Constants.CLIENT_LOGIN_STATUS == false || Constants.USER_LOGIN_STATUS == false) {
			Log.i(TAG, "User hasn't logged in. How did he enter this page at the first place?");
			
			// User and Client are not recognized, go back to previous page and finish current activity.
			Toast.makeText(this, "Oops! Have you logged in?", Toast.LENGTH_SHORT).show();
			this.finish();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.all_music_button:
			this.restMusicDownloader = new RestMusicDownloader(this.melOnClientRestTemplate, this);
			this.restMusicDownloader.execute(MelonApiURLsTools.getDailyChartURL(0, 10));
			this.progressDialog = ProgressDialog.show(this, "Please wait.","Loading content...");
			break;
		case R.id.new_music_button:
			break;
		case R.id.daily_chart_music_button:
			this.restMusicDownloader = new RestMusicDownloader(this.melOnClientRestTemplate, this);
			this.restMusicDownloader.execute(MelonApiURLsTools.getDailyChartURL(0, 10));
			this.progressDialog = ProgressDialog.show(this, "Please wait.","Loading content...");
			break;
		case R.id.weekly_chart_music_button:
			this.restMusicDownloader = new RestMusicDownloader(this.melOnClientRestTemplate, this);
			this.restMusicDownloader.execute(MelonApiURLsTools.getWeeklyChartLatestURL(0, 10));
			this.progressDialog = ProgressDialog.show(this, "Please wait.","Loading content...");
			break;
		case R.id.monthly_chart_music_button:
			this.restMusicDownloader = new RestMusicDownloader(this.melOnClientRestTemplate, this);
			this.restMusicDownloader.execute(MelonApiURLsTools.getMonthlyChartLatestURL(0, 10));
			this.progressDialog = ProgressDialog.show(this, "Please wait.","Loading content...");
			break;
		case R.id.search_button:
			String keyword = keywordBox.getText().toString();
			if (keyword.length() == 0 || keyword == null) keyword = "cinta";
			String genre = genreBox.getText().toString();
			this.restSearchDownloader = new RestSearchDownloader(this.melOnClientRestTemplate, this);
			this.restSearchDownloader.execute(MelonApiURLsTools.getSearchIntegrationSong(0, 10, keyword));
			this.progressDialog = ProgressDialog.show(this, "Please wait.","Searching...");
			break;
		case R.id.genre_box:
			this.restGenreDownloader = new RestGenreDownloader(this.melOnClientRestTemplate, this);
			this.restGenreDownloader.execute(MelonApiURLsTools.getGenreByCategories());
			this.progressDialog = ProgressDialog.show(this, "Please wait.","Loading genres...");
			break;
		}

	}

	@Override
	public void onClientAuthenticationFinished(Object[] result) {
		// TODO Auto-generated method stub
		if (this.progressDialog.isShowing())
			this.progressDialog.dismiss();
		if (result != null) {
			this.melOnClientRestTemplate = (RestTemplate) result[0];
			this.channelCode = (String) result[1];
		}
	}

	@Override
	public void onMusicFinished(String result) {
		// TODO Auto-generated method stub
		// Log.i(TAG + " onMusicFinished()", result);
		if (this.progressDialog.isShowing()) this.progressDialog.dismiss();
		this.musicListView.setAdapter(new MusicAdapter(this, result, melOnClientRestTemplate, melOnUserRestTemplate,channelCode, playerView));
	}

	@Override
	public void onSearchingFinished(String result) {
		// TODO Auto-generated method stub
		// Log.i(TAG + " onSearchingFinished()", result);
		if (this.progressDialog.isShowing()) this.progressDialog.dismiss();
		this.musicListView.setAdapter(new SearchAdapter(this, result));
	}

	@Override
	public void onGenreDownloadFinished(String result) {
		// TODO Auto-generated method stub
		// Log.i(TAG + "onGenreDownloadFinished", result);
		if (this.progressDialog.isShowing()) this.progressDialog.dismiss();
		this.genreChooser = new GenreChooser(this, result, this.genreBox);
		this.genreChooser.showGenreChooserDialog();
	}

	@Override
	public void onServerAuthFinished(RestTemplate result) {
		// TODO Auto-generated method stub
		if(this.progressDialog.isShowing()) this.progressDialog.dismiss();
		if(result != null) this.melOnClientRestTemplate = result;
		
	}

	@Override
	public void onUserDetailDownloadFinished(String result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCertificateDownloadFinished(String result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDownloadFinished(Object object) {
		// TODO Auto-generated method stub
		
	}
}
